package com.somsoft.jtgj;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

import com.baidu.mobstat.StatService;
import com.hjgame.tt.PAManager;

public class MainActivity extends Activity implements OnTouchListener{
	
	private ImageButton btnStart;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        //全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        btnStart = (ImageButton)findViewById(R.id.btnStart); 
        //为图片按钮添加触屏监听
        btnStart.setOnTouchListener(this);
        
        PAManager pm = PAManager.getInstance(this);
		pm.setCooId(this, "abc9ed7a77824436992ee54631acdb11");
		pm.setChannelId(this, "jtgj-androidmarket");
		pm.receiveMessage(this, true);
    }
    
    @Override
	public boolean onTouch(View v, MotionEvent event) {
		//用户当前为按下
		if(event.getAction()==MotionEvent.ACTION_DOWN){
			//设置图片按钮背景图
			btnStart.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_start2));
		//用户当前为抬起
		}else if(event.getAction()==MotionEvent.ACTION_UP){
			btnStart.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_start1));
		}
		Intent it = new Intent(MainActivity.this,LearnActivity.class);
		startActivity(it);
		return false;
	}
    
    @Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
			Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("确认退出学习吗？");  
			builder.setTitle("提示");  
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					MyApplication.getInstance().exit();
				}
			});
			builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			builder.create().show();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}
    
    @Override
	protected void onPause() {
		super.onPause();
		StatService.onPause (this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		StatService.onResume(this);
	}

}